<?php

/**
 * File info.php
 *
 * File containing the iJInfo class
 *
 * This file is part of *** project
 *
 * PHP versions 5
 *
 * @copyright Copyright (c) 2010, ***
 *
 */

  /**
   * iInfo
   *
   * The iInfo class provides information about core functions and brocker versions.
   *
   * @copyright Copyright (c) 2010, ***
   */
  class iJInfo {

    /**
     * function get discription of all calsses contained in DIR_CLASSES
     *
     * @access public
     *
     * @return array of classes disripton
     *
     * @static
     */
    public static function getCoreFunctions() {
      $files = iJScanDir(substr(DIR_CORE, 0, strlen(DIR_CORE) -1));
      $classes = array();
      foreach ($files as $file => $path) {
  
       /* $file = 'customer';
        $path = DIR_CLASSES . 'customer.php';*/
        $ClassName =  strtoupper(substr($file, 0, 1)) . substr($file, 1, strlen($file) - 1);
        if(class_exists($ClassName)) {
          $content =  file_get_contents($path,FILE_TEXT);
         /* $pattern = '/\/\*\*(.*)\*\//isUS';
          preg_match($pattern, $content, $matches);
          if (isset($matches[1])) {
          //  $classes[$ClassName]['file']  = $matches[1];
          } else {
            $classes[$ClassName]['file'] = null;
          }
*/
  //        $cut = strpos($content, '*/') + 2;
    //      $content = substr($content, $cut);

          $pattern = '/' . $ClassName . '(.*)@/isUS';
          preg_match($pattern, $content, $matches);
          if (isset($matches[1])) {
            $classes[$ClassName]['Description']  =  trim(str_replace("*","",$matches[1]));
          } else {
            $classes[$ClassName]['Description'] = null;
          }

          $pattern = '/@copyright(.*)\*/isUS';
          preg_match($pattern, $content, $matches);
          if (isset($matches[1])) {
            $classes[$ClassName]['copyright']  =  trim($matches[1]);
          } else {
            $classes[$ClassName]['copyright'] = null;
          }

          $cut = strpos($content, '*/') + 2;
          $content = substr($content, $cut);

          /*Get description of variables*/
          while (preg_match('/\/\*\*(.*)\*( *)@name/isUS', $content, $description)) {
            $start  = strpos($content, '/**');
            $finish = strpos($content, ';');
            $subcontent = substr($content, $start, $finish - $start + 1);
            preg_match('/@name(.*)\*/isUS',   $subcontent, $name);
            preg_match('/@var(.*)\*/isUS',    $subcontent, $type);
            preg_match('/@access(.*)\*/isUS', $subcontent, $acsess);
            preg_match('/=(.*);/isUS',        $subcontent, $default);


            if(isset($name[1])) {
              $varName = trim($name[1]);
              $classes[$ClassName]['variables'][$varName]['description'] =  trim(str_replace("*","",$description[1]));

              if(isset($type[1])) {
                $classes[$ClassName]['variables'][$varName]['type'] = trim($type[1]);
              }

              if(isset($acsess[1])) {
                $classes[$ClassName]['variables'][$varName]['acsess'] = trim($acsess[1]);
              }

              if(isset($default[1])) {
                $classes[$ClassName]['variables'][$varName]['default'] = trim($default[1]);
              }
              if(strpos($subcontent, 'static') != false) {
                $classes[$ClassName]['variables'][$varName]['static'] = 'true';
              }


            }

            $cut = strpos($content, ';') + 1;
            $content = substr($content, $cut);

          }

          /*Get methods descriprions*/
          $methods = get_class_methods($ClassName);
          foreach ($methods AS  $method) {
       //     svd($method);
            if (preg_match('/function?+[ ]'. $method . '.*\n/isUS', $content, $curMethodPos)) {

              $cur = $curMethodPos[0];

              $pos = strpos($content,$cur);
              $newcont = substr($content, 0, $pos + strlen($cur));
              $pos = strrpos($newcont, '/**');
              $subcontent = substr($newcont,$pos);
              if(!strpos($subcontent, '}')) {


                preg_match('/@access(.*)\*/isUS', $subcontent, $acsess);
                preg_match('/@return(.*)\*/isUS', $subcontent, $return);
                preg_match('/@static(.*)\*/isUS', $subcontent, $static);
                preg_match('/\*(.*)@/isUS',   $subcontent, $descriprion);
                preg_match_all('/@param(.*)\*/isUS', $subcontent, $params);

                if(isset($acsess[1])) {
                    $classes[$ClassName]['methods'][$method]['acsess'] = trim($acsess[1]);
                }

                if(isset($return[1])) {
                    $classes[$ClassName]['methods'][$method]['return'] = trim($return[1]);
                }

                if(isset($descriprion[1])) {
                    $classes[$ClassName]['methods'][$method]['descriptions'] = trim(str_replace("*","",$descriprion[1]));
                }

                if(isset($static[1])) {
                  $classes[$ClassName]['methods'][$method]['static'] = 'true';
                } else {
                  $classes[$ClassName]['methods'][$method]['static'] = 'false';
                }

                if(isset($params[1])) {
                  foreach($params[1] AS $param) {
                    preg_match('/\$(.*) /isUS', $param, $Name);
                    if(isset($Name[0])) {
                      $paramName = trim($Name[0]);
                      preg_match('/(.*)\$/isUS', $param, $paramType);
                      if(isset($paramType[1])) {
                        $classes[$ClassName]['methods'][$method]['params'][$paramName]['type'] = trim($paramType[1]);
                      }
                      preg_match('/\-(.*)$/isUS', $param, $description);
                      if(isset($description[1])) {
                        $classes[$ClassName]['methods'][$method]['params'][$paramName]['descriprion'] = trim($description[1]);
                      }
                      preg_match('/function( *)' . $method . '(.*)\{/isUS', $subcontent, $arguments);
                      if(isset($arguments[0])) {
                        $func_args = $arguments[0];
                        $pname = substr($paramName, 1);

                        preg_match('/\$' . $pname . '(.*)(\)|\,)/isUS', $func_args, $argument);
                        if(isset($argument[1])) {
                          $arg = $argument[1];
                          $arg = str_replace(' ', '', $arg);
                          if($arg != '') {
                            $arg = str_replace('=', '', $arg);
                            $classes[$ClassName]['methods'][$method]['params'][$paramName]['default'] = trim($arg);
                          }
                        }
                      }
                    }

                   /* if(strpos($param, 'optional') != false) {
                      $classes[$ClassName]['methods'][$method]['params'][$paramName]['optional'] = 'true';
                      str_replace('(optional)', '', $param);
                    }

                    if(strpos($param, 'deafult') != false) {
                      $default  = substr($param, strpos($param, '=') + 1);
                      $default = str_replace(')', '', $default);
                      $classes[$ClassName]['methods'][$method]['params'][$paramName]['default'] = trim($default);
                      $param = substr($param, 0, strpos($param, '('));
                    }*/
                  }
                }
              } else {
                $classes[$ClassName]['methods'][$method]['error'] = 'missed desription';
              }

            } else {
              $classes[$ClassName]['methods'][$method]['error'] = 'missed desription';
            }
          }

        }
        break;
      }
      spr($classes); die();
      return $classes;
    }
    
    
    
    /**
     * function get discription of class
     *
     * @access public
     *
     * @return array of classes disripton
     *
     * @static
     */
    public static function getClassDoc($ClassName, $classLocation) {
       
      $classes = array();
      if(strtolower(substr($className, 0, strlen(CLASS_PREFIX))) == strtolower(CLASS_PREFIX)) {
        $className = substr($className, strlen(CLASS_PREFIX), strlen($className) - strlen(CLASS_PREFIX));
      }
      $className = CLASS_PREFIX . $className;     
        $content =  file_get_contents($classLocation,FILE_TEXT);
        $pattern = '/' . $ClassName . '(.*)@/isUS';
        preg_match($pattern, $content, $matches);
        if (isset($matches[1])) {
          $classes['Description']  =  trim(str_replace("*","",$matches[1]));
        } else {
          $classes['Description']  = null;
        }

        $pattern = '/@copyright(.*)\*/isUS';
        preg_match($pattern, $content, $matches);
        if (isset($matches[1])) {
          $classes['copyright']  =  trim($matches[1]);
        } else {
          $classes['copyright'] = null;
        }

        $cut = strpos($content, '*/') + 2;
        $content = substr($content, $cut);

        /*Get description of variables*/
        while (preg_match('/\/\*\*(.*)\*( *)@name/isUS', $content, $description)) {
          $start  = strpos($content, '/**');
          $finish = strpos($content, ';');
          $subcontent = substr($content, $start, $finish - $start + 1);
          preg_match('/@name(.*)\*/isUS',   $subcontent, $name);
          preg_match('/@var(.*)\*/isUS',    $subcontent, $type);
          preg_match('/@access(.*)\*/isUS', $subcontent, $acsess);
          preg_match('/=(.*);/isUS',        $subcontent, $default);


          if(isset($name[1])) {
            $varName = trim($name[1]);
            $classes['variables'][$varName]['description'] =  trim(str_replace("*","",$description[1]));

            if(isset($type[1])) {
              $classes['variables'][$varName]['type'] = trim($type[1]);
            }

            if(isset($acsess[1])) {
              $classes['variables'][$varName]['acsess'] = trim($acsess[1]);
            }

            if(isset($default[1])) {
              $classes['variables'][$varName]['default'] = trim($default[1]);
            }
            if(strpos($subcontent, 'static') != false) {
              $classes['variables'][$varName]['static'] = 'true';
            }


          }

          $cut = strpos($content, ';') + 1;
          $content = substr($content, $cut);

        }

        /*Get methods descriprions*/
        $methods = get_class_methods($ClassName);
        foreach ($methods AS  $method) {
     //     svd($method);
          if (preg_match('/function?+[ ]'. $method . '.*\n/isUS', $content, $curMethodPos)) {

            $cur = $curMethodPos[0];

            $pos = strpos($content,$cur);
            $newcont = substr($content, 0, $pos + strlen($cur));
            $pos = strrpos($newcont, '/**');
            $subcontent = substr($newcont,$pos);
            if(!strpos($subcontent, '}')) {


              preg_match('/@access(.*)\*/isUS', $subcontent, $acsess);
              preg_match('/@return(.*)\*/isUS', $subcontent, $return);
              preg_match('/@static(.*)\*/isUS', $subcontent, $static);
              preg_match('/\*(.*)@/isUS',   $subcontent, $descriprion);
              preg_match_all('/@param(.*)\*/isUS', $subcontent, $params);

              if(isset($acsess[1])) {
                  $classes['methods'][$method]['acsess'] = trim($acsess[1]);
              }

              if(isset($return[1])) {
                  $classes['methods'][$method]['return'] = trim($return[1]);
              }

              if(isset($descriprion[1])) {
                  $classes['methods'][$method]['descriptions'] = trim(str_replace("*","",$descriprion[1]));
              }

              if(isset($static[1])) {
                $classes['methods'][$method]['static'] = 'true';
              } else {
                $classes['methods'][$method]['static'] = 'false';
              }

              if(isset($params[1])) {
                foreach($params[1] AS $param) {
                  preg_match('/\$(.*) /isUS', $param, $Name);
                  if(isset($Name[0])) {
                    $paramName = trim($Name[0]);
                    preg_match('/(.*)\$/isUS', $param, $paramType);
                    if(isset($paramType[1])) {
                      $classes['methods'][$method]['params'][$paramName]['type'] = trim($paramType[1]);
                    }
                    preg_match('/\-(.*)$/isUS', $param, $description);
                    if(isset($description[1])) {
                      $classes['methods'][$method]['params'][$paramName]['descriprion'] = trim($description[1]);
                    }
                    preg_match('/function( *)' . $method . '(.*)\{/isUS', $subcontent, $arguments);
                    if(isset($arguments[0])) {
                      $func_args = $arguments[0];
                      $pname = substr($paramName, 1);

                      preg_match('/\$' . $pname . '(.*)(\)|\,)/isUS', $func_args, $argument);
                      if(isset($argument[1])) {
                        $arg = $argument[1];
                        $arg = str_replace(' ', '', $arg);
                        if($arg != '') {
                          $arg = str_replace('=', '', $arg);
                          $classes['methods'][$method]['params'][$paramName]['default'] = trim($arg);
                        }
                      }
                    }
                  }

                 /* if(strpos($param, 'optional') != false) {
                    $classes['methods'][$method]['params'][$paramName]['optional'] = 'true';
                    str_replace('(optional)', '', $param);
                  }

                  if(strpos($param, 'deafult') != false) {
                    $default  = substr($param, strpos($param, '=') + 1);
                    $default = str_replace(')', '', $default);
                    $classes['methods'][$method]['params'][$paramName]['default'] = trim($default);
                    $param = substr($param, 0, strpos($param, '('));
                  }*/
                }
              }
            } else {
              $classes['methods'][$method]['error'] = 'missed desription';
            }

          } else {
            $classes['methods'][$method]['error'] = 'missed desription';
          }
        }

      return $classes;
    }
    
    

    /**
     * function get brocker version by it code
     *
     * @access public
     *
     * @param string $code - brocker code
     *
     * @return array of brocker version and stage
     *
     * @static
     */
    public static function getBrokckerVersion($code) {
        
    }
    
    public static function getClassInfo($className) {
      $classInfo = array();
      $location  = iJFactory::getLocation($className);
      if($location == false) {
        return;
      }
      
      $content           = file_get_contents($location);
      $content           = str_replace('<?php', '', $content);
      $content           = str_replace('?>',    '', $content);
      
      $classInfo['code'] = htmlspecialchars($content);
      $classInfo['doc']  = self::getClassDoc($className, $location);
      return $classInfo;
      
    }

  }
?>